package com.iwayin.core.utils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class AtUserUtil {
	/**
	 * 获取at用户列表
	 * 
	 * @param atUserNameList
	 *            at用户列表
	 * @param str
	 * @author gyf
	 */
	public static void cutStr(List<String> atUserNameList, String str) {
		String userName = "";
		Pattern p = Pattern.compile("@[A-Za-z0-9\u4E00-\u9FA5]+\\x20|@[A-Za-z0-9\u4E00-\u9FA5]+");
		Matcher m = p.matcher(str);
		if (m.find()) {
			userName = m.group(0);
			userName = userName.replace("@", "");
			str = str.substring(str.indexOf(m.group(0)) + 1);
			if (!atUserNameList.contains(userName)) {
				atUserNameList.add(userName);
			}
			cutStr(atUserNameList, str);
		}
	}

	/**
	 * at用户列表
	 * 
	 * @param str
	 *            微薄内容
	 * @return
	 * @author gyf
	 */
	public static List<String> getAtUserNameList(String str) {
		List<String> atUserNameList = new ArrayList<String>();
		cutStr(atUserNameList, str);
		return atUserNameList;
	}

	public static String atUserName(String str,Map<String,String> userMap) {
		List<String> atUserNameList = new ArrayList<String>();
		cutStr(atUserNameList, str);
		for (String s : atUserNameList) {
			String temp = "@" + s;
			if(null == userMap.get(s)){
				str = str.replace(temp, "<a usercard_name="+s+" href=\"javascript:void(0)\">" + temp + "</a>");
			}else{
				str = str.replace(temp, "<a href='###' onclick='skipYourHome("+"\""+userMap.get(s)+"\""+")'"+">" + temp + "</a>");
			}
		}
		return str;
	}
}
